package com.doudou.time.redislua.controller;

import com.alibaba.fastjson.JSON;
import com.doudou.time.redislua.config.LuaScript;
import com.doudou.time.redislua.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @author yonghao.tang
 * @version 1.0.0
 * @ClassName RedisLuaController.java
 * @Description 测试lua
 * @createTime 2022年11月06日 16:11:00
 */
@Slf4j
@RestController
public class RedisLuaController {

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/get/{key}")
    public String get(@PathVariable String key) {
        return (String) redisUtil.get(key);
    }

    @GetMapping("/set/{key}/{value}")
    public String set(@PathVariable String key, @PathVariable String value) {
        redisUtil.set(key, value, 3600);
        return (String) redisUtil.get(key);
    }


    @Resource(name = "redisScript")
    private DefaultRedisScript<Boolean> redisScript;

    @Resource(name = "redisScriptBatch")
    private DefaultRedisScript<List> redisScriptBatch;

    @Resource(name = "redisScriptBatch2")
    private DefaultRedisScript<HashMap<String, String>> redisScriptBatch2;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 测试lua脚本
     *
     * @return
     */
    @GetMapping("/script/lua/test1")
    public ResponseEntity lua1() {
        List<String> keys = Arrays.asList("test-lua-keys");
        Boolean execute = stringRedisTemplate.execute(redisScript, keys, "test-lua-keys-value", "1000");
        assert execute != null;
        return ResponseEntity.ok(execute);
    }

    /**
     * 测试lua脚本
     *
     * @return
     */
    @GetMapping("/script/lua/test2")
    public ResponseEntity lua2() {
        List<String> keys = Arrays.asList("test-lua-key1", "test-lua-key2", "test-lua-key3", "test-lua-key4", "test-lua-key5");
        Map argvValMap = new HashMap();
        keys.forEach(k -> argvValMap.put(k, k + "val"));
        Map argvMap = new HashMap();
        argvMap.put("expire", 10000);
        argvMap.put("times", 10);
        List<String> result = stringRedisTemplate.execute(redisScriptBatch, keys, JSON.toJSONString(argvValMap), JSON.toJSONString(argvMap));
        Map redisRes = new HashMap(16);
        if (result != null && result.size() > 0) {
            redisRes = JSON.parseObject(result.get(0));
        }
        assert result != null;
        return ResponseEntity.ok(redisRes);
    }

    /**
     * 测试lua脚本
     *
     * @return
     */
    @GetMapping("/script/lua/test3")
    public ResponseEntity lua3() {
        List<String> keys = Arrays.asList("test-lua-key6", "test-lua-key7", "test-lua-key8", "test-lua-key9", "test-lua-key10");
        Map argvValMap = new HashMap();
        keys.forEach(k -> argvValMap.put(k, k + "val"));
        Map argvMap = new HashMap();
        argvMap.put("expire", 10000);
        argvMap.put("times", 10);
        Map<String, String> result = stringRedisTemplate.execute(redisScriptBatch2, keys, JSON.toJSONString(argvValMap), JSON.toJSONString(argvMap));
        System.out.print("result : ");
        System.out.println(result);
        assert result != null;
        return ResponseEntity.ok(result);
    }

    /**
     * 测试lua脚本4
     *
     * @return
     */
    @GetMapping("/script/lua/test4")
    public ResponseEntity lua4() {
        List<String> keys = Arrays.asList("test-lua-key5");
        String luaScript = LuaScript.luaScriptGetBuild();
        log.info("luaScript:\n{}", luaScript);
        RedisScript<String> redisScript = new DefaultRedisScript<>(luaScript, String.class);
        String result = stringRedisTemplate.execute(redisScript, keys, "");
        log.info("result:{}", result);
        assert result != null;
        return ResponseEntity.ok(result);
    }

}
